﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GQ.BLL.QuanLyKho;
using GQ.Utils.TreeNode;
using GQ.WinForms;
using GQ.Utils;
namespace GQ.QuanLyKho
{
    public partial class frmNhanVien : frmBase
    {
        public NhanVien NhanVien;
        ValidateCollection validates = new ValidateCollection();
        public frmNhanVien()
        {
            InitializeComponent();
            validates.Add(new ValidateEmpty(txtMa, "Mã không được để trống", dxErrorProvider1));
            validates.Add(new ValidateEmpty(txtTen, "Tên không được để trống", dxErrorProvider1));
            validates.Add(new ValidateRange(dNgaySinh, new DateTime(1900, 1, 1), new DateTime(9999, 1, 1), ValidationOperator.GreaterThanEqual, ValidationDataType.Date, dxErrorProvider1));
            validates.Add(new ValidateRange(dCmndNgayCap, new DateTime(1900, 1, 1), new DateTime(9999, 1, 1), ValidationOperator.GreaterThanEqual, ValidationDataType.Date, dxErrorProvider1));
            validates.Add(new ValidateEmail(txtEmail, "Email không hợp lệ", dxErrorProvider1));
            
        }
        private void Set()
        {
            txtMa.Text = NhanVien.Ma;
            txtTen.Text = NhanVien.Ten;
            txtMaSoThue.Text = NhanVien.MaSoThue;
            txtChucVu.Text = NhanVien.ChucVu;
            txtCmndNoiCap.Text = NhanVien.CMNDNoiCap;
            txtDiaChi.Text = NhanVien.DiaChi;
            txtDTCoQuan.Text = NhanVien.DienThoaiCoQuan;
            txtDTDiDong.Text = NhanVien.DienThoaiDiDong;
            txtDTNhaRieng.Text = NhanVien.DienThoaiNhaRieng;
            txtEmail.Text = NhanVien.Email;
            txtNHTaiKhoan.Text = NhanVien.NganHangTaiKhoan;
            txtNHTen.Text = NhanVien.NganHangTen;
            txtSoCMND.Text = NhanVien.CMNDSo;
            chNam.Checked = NhanVien.GioiTinh.Value;
            cboPhongban.SetValue(NhanVien.PhongBanID);
            if (NhanVien.CMNDNgayCap != null)
                dCmndNgayCap.DateTime = NhanVien.CMNDNgayCap.Value;
            else dCmndNgayCap.EditValue = null;
            if (NhanVien.NgaySinh != null)
                dNgaySinh.DateTime = NhanVien.NgaySinh.Value;
            else dNgaySinh.EditValue = null;

        }
        private void Get()
        {
            NhanVien.Ma = txtMa.Text;
            NhanVien.Ten = txtTen.Text;
            NhanVien.MaSoThue = txtMaSoThue.Text;
            NhanVien.ChucVu = txtChucVu.Text;
            NhanVien.CMNDNoiCap = txtCmndNoiCap.Text;
            NhanVien.DiaChi = txtDiaChi.Text;
            NhanVien.DienThoaiCoQuan = txtDTCoQuan.Text;
            NhanVien.DienThoaiDiDong = txtDTDiDong.Text;
            NhanVien.DienThoaiNhaRieng = txtDTNhaRieng.Text;
            NhanVien.Email = txtEmail.Text;
            NhanVien.NganHangTaiKhoan = txtNHTaiKhoan.Text;
            NhanVien.NganHangTen = txtNHTen.Text;
            NhanVien.CMNDSo = txtSoCMND.Text;
            PhongBan phongban = (PhongBan)cboPhongban.Value;            
            NhanVien.PhongBanID = phongban.ID;
            NhanVien.GioiTinh = chNam.Checked;
            if (dCmndNgayCap.EditValue != null)
                NhanVien.CMNDNgayCap = dCmndNgayCap.DateTime;
            if (dCmndNgayCap.EditValue != null)
                NhanVien.NgaySinh = dNgaySinh.DateTime;
        }

        private void LoadPhongBan()
        {
            IList<PhongBan> phongs = PhongBan.LoadAllBy("CompanyID={0} and PhongBanChaID={1} Sortby Stt",
                Config.CompanyID, Guid.Empty);
            NodeCollection nodes = new NodeCollection();

            foreach (PhongBan item in phongs)
            {
                nodes.Add(item);
            }
            cboPhongban.LoadDataTree(nodes);

        }
        private void frmNhanVien_Load(object sender, EventArgs e)
        {
            LoadPhongBan();
            if (NhanVien == null)
                NhanVien = new NhanVien();
            Set();

        }

        private void btnSave_CheckedChanged(object sender, EventArgs e)
        {
            if (cboPhongban.Value == null)
            {
                Box.Show("Phòng ban không được để trống");
                return;
            }
            Get();
            NhanVien.Save();
            DialogResult = DialogResult.OK;
        }

        private void chNam_CheckedChanged(object sender, EventArgs e)
        {
            chNu.CheckedChanged -= chNu_CheckedChanged;
            chNu.Checked = !chNam.Checked;
            chNu.CheckedChanged += chNu_CheckedChanged;
        }

        private void chNu_CheckedChanged(object sender, EventArgs e)
        {
            chNam.CheckedChanged -= chNam_CheckedChanged;
            chNam.Checked = !chNu.Checked;
            chNam.CheckedChanged += chNam_CheckedChanged;

        }
    }
}
